package com.fy.tt.executor.node.word.noun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fy.tt.action.TemplateTool;
import com.fy.tt.remote.service.CoreRemoteService;
import com.fy.tt.utils.BandCore;
import lombok.Data;

import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Data
public class User extends Noun {

    CoreRemoteService coreRemoteService = null;

    private String userID;
    private List<String> roles;
    private String userAccount;
    private String text;
    private String role;
    private String id;
    private String name;
    {
        try {
            coreRemoteService = TemplateTool.getBean(CoreRemoteService.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public User(String text, Map<String, Object> scriptEnvironment) {
        init(text, scriptEnvironment);
        //这两句用于测试鉴权
        id=userID;
        role="管理员";
        name=text.trim();
    }

    private void init(String text, Map<String, Object> scriptEnvironment) {
        if(text.startsWith("\"")){
            text = text.substring(1, text.length() - 1);
        }else{
            text = text.trim();
        }

        try {
            JSONObject jsonObject = coreRemoteService.getUserByUserName(text);
            if (((int) jsonObject.get("total")) == 0) {
                throw new RuntimeException("查不到该用户信息：" + text);
            }
            JSONArray rows = jsonObject.getJSONArray("rows");
            String userID = (String) rows.getJSONObject(0).get("userID");
            String userAccount = (String) rows.getJSONObject(0).get("userAccount");
            this.userID = userID;
            this.userAccount = userAccount;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean run(String methodName) {
        return super.run(methodName);
    }

    public static void main(String[] args) throws Exception {
        String text = "梁杰潮";
        // 按照用户名去查询用户信息，并且使用状态为 NORMAL
        String query = "{\"userName\":\"%s\",\"userStatus\": \"NORMAL\"}";
        query = String.format(query, text);
        String encode = URLEncoder.encode(query, "UTF-8");
        String httpurl = "https://www.wetoband.com/core//core/v4/user?format=json&query=%s&aid=21419389378723787447";
        String accessToken = BandCore.getAccessToken();
        httpurl = String.format(httpurl, encode);
        String s = BandCore.doGetWithAccessToken(httpurl, accessToken);
        System.out.println(s);
        JSONObject jsonObject = JSON.parseObject(s);
        if (((int) jsonObject.get("total")) == 0) {
            throw new RuntimeException("查不到该帮区信息：" + text);
        }
        JSONArray rows = jsonObject.getJSONArray("rows");
        String userID = (String) rows.getJSONObject(0).get("userID");
        System.out.println("userID = " + userID);
    }



}
